package org.example.admin.flight;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class updateFlightInfoView extends JFrame {
    private JTextField flightIdField,flightNumField,airwayIdField,departureTimeField,arrivalTimeField,aircraftIdField,priceField,availableSeatField;


    private JButton updateButton;

    public updateFlightInfoView() {
        setTitle("管理端航班信息修改页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(11, 2));

        JLabel flightIdLabel = new JLabel("航班id");
        flightIdField = new JTextField();

        JLabel flightNumLabel = new JLabel("航班数量");
        flightNumField = new JTextField();

        JLabel airwayIdLabel = new JLabel("航线id");
        airwayIdField = new JTextField();

        JLabel departureTimeLabel = new JLabel("出发时间");
        departureTimeField = new JTextField();

        JLabel  arrivalTimeLabel = new JLabel("到达时间");
        arrivalTimeField = new JTextField();

        JLabel  aircraftIdLabel = new JLabel("飞机id");
        aircraftIdField = new JTextField();

        JLabel  priceLabel = new JLabel("票价");
        priceField = new JTextField();

        JLabel  availableSeatLabel = new JLabel("可预订座位");
        availableSeatField = new JTextField();
        updateButton = new JButton("更新");

        // 添加组件到窗口
        add(flightIdLabel);
        add(flightIdField);

        add(flightNumLabel);
        add(flightNumField);

        add(airwayIdLabel);
        add(airwayIdField);

        add(departureTimeLabel);
        add(departureTimeField);

        add(arrivalTimeLabel);
        add(arrivalTimeField);

        add(aircraftIdLabel);
        add(aircraftIdField);

        add(priceLabel);
        add(priceField);

        add(availableSeatLabel);
        add(availableSeatField);


        add(new JLabel()); // 占位
        add(updateButton);

        // 添加更新按钮的点击事件监听器
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateUserInfo();
            }
        });
    }

    private void updateUserInfo() {
        int flightId = Integer.parseInt(flightIdField.getText());
        String flightNum=flightNumField.getText();
        String aircraftId=aircraftIdField.getText();
        String departureTime = departureTimeField.getText();
        String arrivalTime=arrivalTimeField.getText();
        int  airwayId = Integer.parseInt(airwayIdField.getText());
        String price = priceField.getText();
        int availableSeat = Integer.parseInt(availableSeatField.getText());


        try {
            // 连接数据库
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");

            // 创建更新语句
            String updateQuery = "UPDATE air_flight SET flight_num=?,airway_id=?,departure_time=?,arrival_time=?,aircraft_id=?,price=?,available_seat=? WHERE flight_id=? ";
            PreparedStatement statement = conn.prepareStatement(updateQuery);


            statement.setString(1, flightNum);
            statement.setString(2, aircraftId);
            statement.setString(3, departureTime);
            statement.setString(4, arrivalTime);
            statement.setInt(5, airwayId);
            statement.setString(6, price);
            statement.setInt(7, availableSeat);
            statement.setInt(8, flightId);


            // 执行更新操作
            int rowsUpdated = statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            conn.close();

            if (rowsUpdated > 0) {
                JOptionPane.showMessageDialog(this, "更新成功");
            } else {
                JOptionPane.showMessageDialog(this, "用户不存在或更新失败");
            }

            // 清空输入框
            clearFields();

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "更新失败：" + e.getMessage());
        }
    }

    private void clearFields() {
        flightIdField.setText("");
        flightNumField.setText("");
        airwayIdField.setText("");
        departureTimeField.setText("");
        arrivalTimeField.setText("");
        aircraftIdField.setText("");
        priceField.setText("");
        availableSeatField.setText("");

    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            updateFlightInfoView updateFlightInfoForm = new updateFlightInfoView();
//            updateFlightInfoForm.setSize(300, 150);
//            updateFlightInfoForm.setVisible(true);
//        });
    }
}